import { getFollowedList, getFansList } from '../../../api/user'

Page({
  data: {
    activeName: 'followed',
    isFollowedEnd: false,
    followedList: [],
    followedQuery: {
      condition: {
        keyword: ''
      },
      pager: {
        page: 1,
        pageSize: 10
      }
    },
    isFansEnd: false,
    fansList: [],
    fansQuery: {
      pager: {
        page: 1,
        pageSize: 10
      }
    },
    links: [
      { name: '相互关注', src: '/static/imgs/mutual.png', url: '/packageA/pages/contactsEach/contacts' },
      { name: '特别关注', src: '/static/imgs/special.png', url: '/packageA/pages/contactsSpecial/contacts' },
      { name: '我的分组', src: '/static/imgs/group.png', url: '/packageA/pages/contactsGroup/contacts' },
    ]
  },

  onLoad: function (options) {
    this.getFollowedList()
    this.getFansList()
  },

  getFollowedList() {
    if (!this.data.isFollowedEnd) {
      getFollowedList(this.data.followedQuery).then(res => {
        if (res.records.length) {
          let list = this.data.followedList
          list = list.concat(res.records)
          this.setData({
            followedList: list
          })
        }
        if (res.records.length < 10) {
          this.setData({
            isFollowedEnd: true
          })
        }
      })
    }
  },

  getMoreFollowed() {
    const page = this.data.followedQuery.pager.page++
    this.setData({
      'followedQuery.pager.page': page
    })
    this.getFollowedList()
  },

  searchFollowed(e) {
    this.setData({
      'followedQuery.condition.keyword': e.detail,
      'followedQuery.pager.page': 1,
      followedList: [],
      isFollowedEnd: false
    })
    this.getFollowedList()
  },

  getFansList() {
    if (!this.data.isFansEnd) {
      getFansList(this.data.fansQuery).then(res => {
        if (res.records.length) {
          let list = this.data.fansList
          list = list.concat(res.records)
          this.setData({
            fansList: list
          })
        }
        if (res.records.length < 10) {
          this.setData({
            isFansEnd: true
          })
        }
      })
    }
  },

  getMoreFans() {
    const page = this.data.fansQuery.pager.page++
    this.setData({
      'fansQuery.pager.page': page
    })
    this.getFansList()
  },

  searchFans(e) {
    this.setData({
      'fansQuery.condition.keyword': e.detail,
      'fansQuery.pager.page': 1,
      'fansList': [],
      isFansEnd: false
    })
    this.getFansList()
  },

  tapTabs(e) {
    this.setData({
      activeName: e.detail.name
    })
  },

  toLower() {
    if (this.data.activeName === 'followed') {
      this.getMoreFollowed()
    } else {
      this.getMoreFans()
    }
  },

  toLink(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  }
})
